﻿using AdinZ.DB;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;


/**
* @Author: AdinZ
* @Q  Q : 1454269
* @Email:adinz@adinz.com
* @Created: 2014/4/13 22:23:24
*
* */
namespace Novel.Sys.Dal
{
    public class novel_book_orders
    {
        /// <summary>
        /// 添加一个订单
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public int Add(Model.novel_book_orders model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO [novel_book_orders] ([uid], [bookid], [chapterid], [payment], [price], [time], [status]) VALUES (");
            strSql.Append("@uid,@bookid,@chapterid,@payment,@price,@time,@status");
            strSql.Append(");select @@IDENTITY");
            SqlParameter[] parameters = {
                    new SqlParameter("@uid", SqlDbType.Int),
                    new SqlParameter("@bookid", SqlDbType.Int),
                    new SqlParameter("@chapterid", SqlDbType.BigInt),
                    new SqlParameter("@payment", SqlDbType.Float),
                    new SqlParameter("@price", SqlDbType.Float),
                    new SqlParameter("@time", SqlDbType.DateTime),
                    new SqlParameter("@status", SqlDbType.Int)};
            parameters[0].Value = model.Uid;
            parameters[1].Value = model.BookId;
            parameters[2].Value = model.ChapterId;
            parameters[3].Value = model.Payment;
            parameters[4].Value = model.Price;
            parameters[5].Value = model.Time;
            parameters[6].Value = model.Status;
            object obj = DbHelpers.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }



        public bool Exists(int uid, int bookid)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [novel_book_orders]");
            strSql.Append(" where uid=@uid and bookid=@bookid");
            SqlParameter[] parameters = {
                    new SqlParameter("@uid", SqlDbType.Int),
                    new SqlParameter("@bookid", SqlDbType.Int)};
            parameters[0].Value = uid;
            parameters[1].Value = bookid;
            return DbHelpers.Exists(strSql.ToString(), parameters);
        }

    }
}
